package top.yongyan0624.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import top.yongyan0624.pojo.Lesson;
import top.yongyan0624.service.LessonService;

import java.util.List;

/**
 * @author Yongyan
 * @description: TODO
 * @date 2020/12/23
 **/

@Controller
@RequestMapping("/lesson")
@SessionAttributes("error")
public class LessonController {

    @Autowired
    private LessonService lessonService;

    @RequestMapping("/toAddLessonPage")
    public String toAddLessonPage(Model model){
        List<Lesson> lessonList = lessonService.findAllLesson();
        model.addAttribute("lessonList",lessonList);
        model.addAttribute("error","");

        return "addLessonPage";
    }

    @RequestMapping("/addLesson")
    public String addLesson(Lesson lesson, Model model){
        Lesson c = lessonService.findById(lesson.getLno());
        if (c == null){
            lessonService.addLesson(lesson);
        } else {
            model.addAttribute("error","该课程编号已被录入！");
            return "addLessonPage";
        }
        return "redirect:/lesson/toFindAllLessonPage/1/5";
    }

    @RequestMapping("/deleteLesson/{id}")
    public String deleteLesson(@PathVariable("id") Integer id){
        lessonService.deleteLesson(id);
        return "redirect:/lesson/toFindAllLessonPage/1/5";
    }

    @RequestMapping("/toUpdateLessonPage/{id}")
    public String toUpdateLessonPage(@PathVariable Integer id, Model model){
        Lesson lesson = lessonService.findById(id);
        model.addAttribute("lesson",lesson);
        List<Lesson> lessonList = lessonService.findAllLesson();
        model.addAttribute("lessonList",lessonList);

        return "updateLessonPage";
    }

    @RequestMapping("/updateLesson")
    public String updateLesson(Lesson lesson){
        lessonService.updateLesson(lesson);
        return "redirect:/lesson/toFindAllLessonPage/1/5";
    }

    @RequestMapping("/toFindAllLessonPage/{pageNum}/{pageSize}")
    public String toFindAllPage(@PathVariable(value = "pageNum") Integer pageNum, @PathVariable(value = "pageSize") Integer pageSize,Model model){
        PageInfo<Lesson> pageInfo = lessonService.findAllLesson(pageNum, pageSize);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("lessonList",pageInfo.getList());
        model.addAttribute("error","");

        return "findAllLessonPage";
    }

    @RequestMapping("/findLesson")
    public String findLesson(Lesson lesson,Model model){
        List<Lesson> lessonList = lessonService.findLesson(lesson);

        if (lessonList.size()<=0){
            model.addAttribute("error","未找到该课程...");

        } else if (lesson.getLno() == null && lesson.getName().equals("")) {
            model.addAttribute("error","");

        } else {
            model.addAttribute("lessonList",lessonList);
            model.addAttribute("error","");
            return "findAllLessonPage";
        }

        return "redirect:/lesson/toFindAllLessonPage/1/5";
    }

}
